<?php
/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package core
 * @subpackage event
 */
class BXD_Event_Manager
{
	private static $instance = null;

	private $eventsListener = array();

	private function __construct()
	{
		$this->eventsListener = array();
	}

	/**
	 * Ottiene l'istanza dell'event manager
	 * 
	 * @return BXD_Event_Manager l'istanza dell'event manager
	 */

	public static function getInstance()
	{
		if (self::$instance == null)
			self::$instance = new BXD_Event_Manager();

		return self::$instance;
	}

	/**
	 * Aggiunge un listener ad un evento
	 *
	 * @param string $eventNotify
	 * @param BXD_Event_Interface_Listener $eventListener
	 */

	public function addEventListener($eventNotify, BXD_Event_Interface_Listener $eventListener)
	{
		if (!isset($this->eventsListener[$eventNotify]))
			$this->eventsListener[$eventNotify] = array();

		$this->eventsListener[$eventNotify][] = $eventListener;
	}

	/**
	 * Aggiunge un listener ad un evento
	 *
	 * @param string $eventNotify
	 * @param BXD_Event_Interface_Listener $eventListener
	 */

	public function addCallback($eventNotify, $class, $method, $params = null)
	{
		if(!is_object($class))
			throw new BXD_Event_Exception("", BXD_Event_Exception::CLASS_IS_NOT_OBJECT);

		if(!is_callable(array($class, $method)))
			throw new BXD_Event_Exception(array($method, get_class($class)), BXD_Event_Exception::METHOD_NOT_CALLABLE);

		if (!isset($this->eventsListener[$eventNotify]))
			$this->eventsListener[$eventNotify] = array();

		$this->eventsListener[$eventNotify][] = array($class, $method, $params);
	}

	/**
	 * Genera un nuovo evento, passando i parametri necessari.
	 *
	 * @param string $eventNotify il nome dell'evento da generare
	 * @param mixed $eventParams i parametri dell'evento. Dipendono dal tipo di evento anche per il formato
	 */

	public function notify($eventNotify, $eventParams)
	{
		$events = array();

		if (isset($this->eventsListener[$eventNotify]))
		{
			foreach ($this->eventsListener[$eventNotify] as $event)
			{
				if($event instanceof BXD_Event_Interface_Listener)
				{
					$event->onAction($eventParams);
				}
				else
				{
					$params = array();

					if($event[2] !== null)
					{
						if(is_array($event[2]))
						{
							$params = $event[2];
						}
						else
						{
							$params = array($event[2]);
						}
					}

					call_user_func_array(array($event[0], $event[1]), $params);
				}
			}
		}

	}

	public function isExist($eventNotified)
	{
		if (isset($this->eventsListener[$eventNotify]))
			return true;

		return false;
	}
}
?>